﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Windows.Media;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Styx;
using Styx.Helpers;
using Styx.Common;
using Styx.CommonBot.POI;
using Styx.CommonBot;
using Styx.CommonBot.Routines;
using Styx.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.TreeSharp;
using Action = Styx.TreeSharp.Action;

namespace ForstDoeninge
{
    public partial class Classname : CombatRoutine
    {
        public override sealed string Name { get { return "Frost Mage by Pasterke"; } }

        public override WoWClass Class { get { return WoWClass.Mage; } }

        private static LocalPlayer Me { get { return StyxWoW.Me; } }

        public override bool WantButton { get { return true; } }

        public override void OnButtonPress()
        {
            Form1 ConfigForm = new Form1();
            ConfigForm.ShowDialog();
        }
        public override void Initialize()
        {

        }
        public override void Pulse()
        {
        }
        #region CC_Begin

        #region rest
        public override bool NeedRest
        {
            get
            {
                if ((Me.HealthPercent <= CRSettings.myPrefs.EatDrink
                    || Me.ManaPercent <= CRSettings.myPrefs.EatDrink)
                    && !Me.IsSwimming
                    && !Me.IsDead
                    && !Me.IsGhost
                    && !Me.IsInInstance
                    && !Me.GroupInfo.IsInRaid)
                {
                    return true;
                }
                return false;
            }
        }

        public override void Rest()
        {
            if ((Me.HealthPercent <= CRSettings.myPrefs.EatDrink
                || Me.ManaPercent <= CRSettings.myPrefs.EatDrink)
                && !Me.IsSwimming
                && !Me.IsDead
                && !Me.IsGhost
                && !Me.IsInInstance
                && !Me.GroupInfo.IsInRaid)
            {
                Styx.CommonBot.Rest.Feed();
            }
        }

        #endregion rest

        public override bool NeedPullBuffs { get { return Buffs(); } }
        public override bool NeedPreCombatBuffs { get { return Buffs(); } }
        public override bool NeedCombatBuffs { get { return Buffs(); } }

        #region Pull

        public override void Pull()
        {
            if (gotTarget
                && Me.CurrentTarget.Distance > CRSettings.myPrefs.range)
            {
                Navigator.MoveTo(StyxWoW.Me.CurrentTarget.Location);
                return;
            }
            if (gotTarget
                && !Me.CurrentTarget.InLineOfSight)
            {
                Navigator.MoveTo(StyxWoW.Me.CurrentTarget.Location);
                return;
            }
            if (gotTarget
                && Me.CurrentTarget.Distance <= CRSettings.myPrefs.range
                && Me.CurrentTarget.InLineOfSight)
            {
                Navigator.PlayerMover.MoveStop();
            }
            if (gotTarget && !CRSettings.myPrefs.Facing)
            {
                Facing();
            }
            if (gotTarget
                && buffExists("Brain Freeze", Me))
            {
                SpellCast("Frostfire Bolt", 6);
            }
            if (gotTarget
                && buffExists("Fingers of Frost", Me))
            {
                SpellCast("Ice Lance", 6);
            }
            if (gotTarget
                && !debuffExists("Nether Tempest", Me.CurrentTarget)
                && LastSpell != "Nether Tempest")
            {
                SpellCast("Nether Tempest", 6);
            }
            if (gotTarget)
            {
                SpellCast("Frostbolt", 6);
            }
            if (gotTarget
                && !SpellManager.HasSpell("frostbolt"))
            {
                SpellCast("Frostfire Bolt", 6);
            }
        }

        #endregion pull

        #region Combat
        public override void Combat()
        {
            if (!Me.Mounted && !Me.IsDead && !Me.IsGhost)
            {
                if (Me.Specialization == WoWSpec.MageFrost)
                {
                    FrostRotation();
                }
            }
        }
        public void FrostRotation()
        {
            if (!Me.GotTarget && (Me.PetInCombat || Me.PetAggro) && !Me.Mounted && !CRSettings.myPrefs.Targetting)
            {
                Me.Pet.CurrentTarget.Target();
            }
            if (!gotTarget && !CRSettings.myPrefs.Targetting)
            {
                FindTarget();
            }
            if (gotTarget && Me.CurrentTarget.Distance >= CRSettings.myPrefs.range && !CRSettings.myPrefs.Movement)
            {
                Navigator.MoveTo(StyxWoW.Me.CurrentTarget.Location);
                return;
            }
            if (gotTarget && !Me.CurrentTarget.InLineOfSight && !CRSettings.myPrefs.Movement)
            {
                Navigator.MoveTo(StyxWoW.Me.CurrentTarget.Location);
                return;
            }
            if (gotTarget && Me.CurrentTarget.Distance <= CRSettings.myPrefs.range && !CRSettings.myPrefs.Movement)
            {
                Navigator.PlayerMover.MoveStop();
            }
            if (gotTarget && !CRSettings.myPrefs.Facing)
            {
                Me.CurrentTarget.Face();
            }

            #region AoeRotation

            if (gotTarget && addCount >= CRSettings.myPrefs.UseAoe)
            {
                if (gotTarget
                    && CRSettings.myPrefs.Counterspell
                    && (Me.CurrentTarget.IsCasting
                    && Me.CanInterruptCurrentSpellCast)
                    && !spellOnCooldown("Counterspell"))
                {
                    SpellCast("Counterspell", 4);
                }
                if (gotTarget 
                    && !debuffExists("Nether Tempest", Me.CurrentTarget) 
                    && LastSpell != "Nether Tempest")
                {
                    SpellCast("Nether Tempest", 6);
                }
                if (gotTarget 
                    && buffExists("Brain Freeze", Me)
                    && IsWoWBoss(Me.CurrentTarget))
                {
                    SpellCast("Frostfire Bolt", 6);
                    return;
                }
                if (gotTarget && buffExists("Fingers of Frost", Me) 
                    && buffStackCount("Fingers of Frost", Me) == 2 
                    && !spellOnCooldown("Deep Freeze")
                    && IsWoWBoss(Me.CurrentTarget))
                {
                    SpellCast("Deep Freeze", 6);
                    return;
                }
                if (gotTarget 
                    && buffExists("Fingers of Frost", Me) 
                    && buffStackCount("Fingers of Frost", Me) >= 1
                    && IsWoWBoss(Me.CurrentTarget))
                {
                    SpellCast("Ice Lance", 6);
                    return;
                }
                if (gotTarget
                    && Me.GotAlivePet
                    && IsInPetRange(40, Me.CurrentTarget)
                    && Me.CurrentTarget.InLineOfSight
                    && CanCastPetAction("Freeze"))
                {
                    CastPetAction("Freeze");
                }
                if (gotTarget
                    && !spellOnCooldown("Frost Nova")
                    && Me.CurrentTarget.Distance <= 12)
                {
                    SpellCast("Frost Nova", 6);
                }
                if (gotTarget
                     && !spellOnCooldown("Icy Veins")
                     && (CRSettings.myPrefs.IcyVeins == 1
                     || (CRSettings.myPrefs.IcyVeins == 2 && IsWoWBoss(Me.CurrentTarget)))
                     && !Me.IsCasting)
                {
                    if (CRSettings.myPrefs.AlterTime == 1)
                    {
                        SpellCast("Alter Time", 5);
                    }
                    SpellCast("Icy Veins", 6);
                    if (CRSettings.myPrefs.AlterTime == 1)
                    {
                        SpellCast("Alter Time", 5);
                    }
                }
                if (gotTarget
                    && CRSettings.myPrefs.jadepotion
                    && buffExists("Icy Veins", Me))
                {
                    useJadePotion();
                }
                if (gotTarget
                    && CRSettings.myPrefs.trinket1
                    && buffExists("Icy Veins", Me))
                {
                    useTrinket1();
                }
                if (gotTarget
                    && CRSettings.myPrefs.trinket2
                    && buffExists("Icy Veins", Me))
                {
                    useTrinket2();
                }
                if (gotTarget
                    && CRSettings.myPrefs.gloves
                    && buffExists("Icy Veins", Me))
                {
                    useEngiGloves();
                }
                if (gotTarget
                    && !spellOnCooldown("Mirror Image")
                    && (CRSettings.myPrefs.MirrorImage == 1
                    || (CRSettings.myPrefs.MirrorImage == 2 && IsWoWBoss(Me.CurrentTarget)))
                    && !Me.IsCasting)
                {
                    SpellCast("Mirror Image", 6);
                }
                if (gotTarget
                    && !spellOnCooldown("Frozen Orb")
                    && (CRSettings.myPrefs.FrozenOrb == 1
                    || CRSettings.myPrefs.FrozenOrb == 2))
                {
                    SpellCast("Frozen Orb", 6);
                }
                if (gotTarget && !spellOnCooldown("Flamestrike"))
                {
                    DropCast("Flamestrike", 6);
                }
                if (gotTarget && !Me.IsChanneling)
                {
                    DropCast("Blizzard", 6);
                }
                if (gotTarget 
                    && UnfriendlyUnits.Count() >= 3)
                {
                    SpellCast("Arcane Explosion", 6);
                }
                if (gotTarget
                    && Me.Level < 12
                    && !Me.IsCasting)
                {
                    SpellCast("Frostfire Bolt", 3);
                }
                if (gotTarget
                    && !SpellManager.HasSpell("Blizzard")
                    && !Me.IsCasting)
                {
                    SpellCast("Frostbolt", 6);
                }
                return;
            }
            #endregion AoeRotation


            #region single target rotation

            if (gotTarget 
                && CRSettings.myPrefs.Counterspell
                && (Me.CurrentTarget.IsCasting 
                && Me.CanInterruptCurrentSpellCast)
                && !spellOnCooldown("Counterspell"))
            {
                SpellCast("Counterspell", 4);
            }
            if (gotTarget && !debuffExists("Nether Tempest", Me.CurrentTarget) && LastSpell != "Nether Tempest")
            {
                SpellCast("Nether Tempest", 1);
            }
            if (gotTarget && buffExists("Brain Freeze", Me))
            {
                SpellCast("Frostfire Bolt", 1);
                return;
            }
            if (gotTarget && buffExists("Fingers of Frost", Me) && buffStackCount("Fingers of Frost", Me) == 2 && !spellOnCooldown("Deep Freeze"))
            {
                SpellCast("Deep Freeze", 1);
                return;
            }
            if (gotTarget && buffExists("Fingers of Frost", Me) && buffStackCount("Fingers of Frost", Me) >= 1)
            {
                SpellCast("Ice Lance", 1);
                return;
            }
            if (gotTarget
                    && Me.GotAlivePet
                    && IsInPetRange(40, Me.CurrentTarget)
                    && Me.CurrentTarget.InLineOfSight
                    && CanCastPetAction("Freeze"))
            {
                CastPetAction("Freeze");
            }
            if (gotTarget
                && Me.Pet.CurrentTarget != null
                    && !spellOnCooldown("Frost Nova")
                    && Me.CurrentTarget.Distance <= 12)
            {
                SpellCast("Frost Nova", 2);
            }
            if (gotTarget
                    && !spellOnCooldown("Icy Veins")
                    && (CRSettings.myPrefs.IcyVeins == 1
                    || (CRSettings.myPrefs.IcyVeins == 2 && IsWoWBoss(Me.CurrentTarget)))
                    && !Me.IsCasting)
            {
                if (CRSettings.myPrefs.AlterTime == 1)
                {
                    SpellCast("Alter Time", 5);
                }
                SpellCast("Icy Veins", 4);
                if (CRSettings.myPrefs.AlterTime == 1)
                {
                    SpellCast("Alter Time", 5);
                }
            }
            if (gotTarget
                && CRSettings.myPrefs.jadepotion
                && buffExists("Icy Veins", Me))
            {
                useJadePotion();
            }
            if (gotTarget
                && CRSettings.myPrefs.trinket1
                && buffExists("Icy Veins", Me))
            {
                useTrinket1();
            }
            if (gotTarget
                && CRSettings.myPrefs.trinket2
                && buffExists("Icy Veins", Me))
            {
                useTrinket2();
            }
            if (gotTarget
                && CRSettings.myPrefs.gloves
                && buffExists("Icy Veins", Me))
            {
                useEngiGloves();
            }
            if (gotTarget
                    && !spellOnCooldown("Mirror Image")
                    && (CRSettings.myPrefs.MirrorImage == 1
                    || (CRSettings.myPrefs.MirrorImage == 2 && IsWoWBoss(Me.CurrentTarget)))
                    && !Me.IsCasting)
            {
                SpellCast("Mirror Image", 4);
            }
            if (gotTarget
                    && !spellOnCooldown("Frozen Orb")
                    && (CRSettings.myPrefs.FrozenOrb == 1
                    || (CRSettings.myPrefs.FrozenOrb == 2 && addCount >= CRSettings.myPrefs.UseAoe)
                    || (CRSettings.myPrefs.FrozenOrb == 3 && IsWoWBoss(Me.CurrentTarget))
                    || (CRSettings.myPrefs.FrozenOrb == 4 && (IsWoWBoss(Me.CurrentTarget) || addCount >= CRSettings.myPrefs.UseAoe))))
            {
                SpellCast("Frozen Orb", 1);
            }
            if (gotTarget
                    && !spellOnCooldown("Fire Blast")
                    && Me.CurrentTarget.Distance <= 30
                    && CRSettings.myPrefs.useFireBlast)
            {
                SpellCast("Fire Blast", 3);
            }
            if (gotTarget
                    && !Me.IsCasting)
            {
                SpellCast("Frostbolt", 3);
            }
            if (gotTarget
                    && Me.Level < 12
                    && !Me.IsCasting
                    && !Me.IsCasting)
            {
                SpellCast("Frostfire Bolt", 3);
            }
            return;

            #endregion single target rotation
        }
        #endregion combat


        #region autoattack


        private void AutoAttack()
        {
            if (!Me.IsAutoAttacking)
            {
                Lua.DoString("StartAttack()");
            }

        }
        #endregion autoattack










        #endregion CC

    }
}
